package com.hungryinlindholmen;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.jdbc.JDBCCategoryDataset;

/**
 * Servlet implementation class ChartServlet
 * 
 * Receives a request along with a parameter & id of current owner logged in
 * from either edit.jsp, owner.jsp or ownerresults.jsp. The different
 * doBarChartStat() requests the id currently logged in, queries the database
 * and creates a chart using the Jfreechart library. The chart is then rendered
 * and responded as an image. The image is sent to whichever .jsp made the
 * request.
 * 
 * @author Ludvig Gee
 * 
 */

@WebServlet("/ChartServlet")
public class ChartServlet extends HttpServlet {

    /**
     * @see HttpServlet#HttpServlet()
     */
    private static final long serialVersionUID = 1L;

    @Override
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws IOException, ServletException {
	String chart = request.getParameter("type");

	if (chart != null) {
	    if (chart.equals("a")) {
		doBarChartStat(request, response);
	    }

	    if (chart.equals("b")) {
		doBarChartStat2(request, response);
	    }

	    if (chart.equals("c")) {
		doBarChartStat3(request, response);
	    }

	    if (chart.equals("d")) {
		doBarChartStat4(request, response);
	    }
	}
    }

    protected void doBarChartStat(HttpServletRequest request,
	    HttpServletResponse response) throws IOException, ServletException {
	String restID = request.getParameter("restId");
	Connection con = null;
	OutputStream out = response.getOutputStream();

	try {
	    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    try {
		con = DriverManager.getConnection(
			"jdbc:mysql://db.student.chalmers.se/", "olofek",
			"Ac6Jkdoa");
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	} catch (InstantiationException e) {
	    e.printStackTrace();
	} catch (IllegalAccessException e) {
	    e.printStackTrace();
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	}

	JDBCCategoryDataset dataset = new JDBCCategoryDataset(con);
	try {

	    dataset.executeQuery("SELECT stats.dish, stats.dish_clicks FROM olofek.stats "
		    + "WHERE rest_id = '"
		    + restID
		    + "' ORDER BY stats.dish_clicks DESC limit 10");

	    JFreeChart chart = ChartFactory.createBarChart3D(
		    "Top 10 Click Dishes", "Dish", "Amount of Clicks", dataset,
		    PlotOrientation.VERTICAL, true, true, false);

	    if (chart != null) {
		int width = 1600;
		int height = 720;
		chart.setAntiAlias(true);
		chart.setBackgroundPaint(null);

		final ChartRenderingInfo info = new ChartRenderingInfo(
			new StandardEntityCollection());

		response.setContentType("image/png");

		ChartUtilities.writeChartAsPNG(out, chart, width, height, info);
	    }
	} catch (SQLException e) {
	    e.printStackTrace();
	}

    }

    protected void doBarChartStat2(HttpServletRequest request,
	    HttpServletResponse response) throws IOException, ServletException {
	Connection con = null;
	OutputStream out = response.getOutputStream();

	try {
	    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    try {
		con = DriverManager.getConnection(
			"jdbc:mysql://db.student.chalmers.se/", "olofek",
			"Ac6Jkdoa");
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	} catch (InstantiationException e) {
	    e.printStackTrace();
	} catch (IllegalAccessException e) {
	    e.printStackTrace();
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	}

	JDBCCategoryDataset dataset_1 = new JDBCCategoryDataset(con);
	try {

	    dataset_1
		    .executeQuery("SELECT searches.keyword, searches.noofsearches FROM olofek.searches "
			    + "ORDER BY searches.noofsearches DESC limit 10");

	    JFreeChart chart = ChartFactory.createBarChart3D("Top 10 Searches",
		    "Keyword", "Number of Searches", dataset_1,
		    PlotOrientation.VERTICAL, true, true, false);

	    if (chart != null) {
		int width = 1600;
		int height = 720;
		chart.setAntiAlias(true);
		chart.setBackgroundPaint(null);

		final ChartRenderingInfo info = new ChartRenderingInfo(
			new StandardEntityCollection());

		response.setContentType("image/png");

		ChartUtilities.writeChartAsPNG(out, chart, width, height, info);
	    }
	} catch (SQLException e) {
	    e.printStackTrace();
	}

    }

    protected void doBarChartStat3(HttpServletRequest request,
	    HttpServletResponse response) throws IOException, ServletException {
	Connection con = null;
	OutputStream out = response.getOutputStream();

	try {
	    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    try {
		con = DriverManager.getConnection(
			"jdbc:mysql://db.student.chalmers.se/", "olofek",
			"Ac6Jkdoa");
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	} catch (InstantiationException e) {
	    e.printStackTrace();
	} catch (IllegalAccessException e) {
	    e.printStackTrace();
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	}

	JDBCCategoryDataset dataset_1 = new JDBCCategoryDataset(con);
	try {

	    dataset_1
		    .executeQuery("SELECT DISTINCT stats.rest_id, stats.rest_clicks FROM olofek.stats "
			    + "ORDER BY stats.rest_clicks DESC limit 10");

	    JFreeChart chart = ChartFactory.createBarChart3D(
		    "Top 10 Restaurants", "Restaurant", "Number of Clicks",
		    dataset_1, PlotOrientation.VERTICAL, true, true, false);

	    if (chart != null) {
		int width = 1600;
		int height = 720;
		chart.setAntiAlias(true);
		chart.setBackgroundPaint(null);

		final ChartRenderingInfo info = new ChartRenderingInfo(
			new StandardEntityCollection());

		response.setContentType("image/png");

		ChartUtilities.writeChartAsPNG(out, chart, width, height, info);
	    }
	} catch (SQLException e) {
	    e.printStackTrace();
	}

    }

    protected void doBarChartStat4(HttpServletRequest request,
	    HttpServletResponse response) throws IOException, ServletException {
	String restID = request.getParameter("restId");
	Connection con = null;
	OutputStream out = response.getOutputStream();

	try {
	    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    try {
		con = DriverManager.getConnection(
			"jdbc:mysql://db.student.chalmers.se/", "olofek",
			"Ac6Jkdoa");
	    } catch (SQLException e) {
		e.printStackTrace();
	    }
	} catch (InstantiationException e) {
	    e.printStackTrace();
	} catch (IllegalAccessException e) {
	    e.printStackTrace();
	} catch (ClassNotFoundException e) {
	    e.printStackTrace();
	}

	JDBCCategoryDataset dataset = new JDBCCategoryDataset(con);
	try {

	    dataset.executeQuery("SELECT stats.dish_lunch, stats.dish_lunch_clicks FROM olofek.stats "
		    + "WHERE rest_id = '"
		    + restID
		    + "' ORDER BY stats.dish_lunch_clicks DESC limit 10");

	    JFreeChart chart = ChartFactory.createBarChart3D(
		    "Top 10 Lunch Dishes", "Dish", "Amount of Clicks", dataset,
		    PlotOrientation.VERTICAL, true, true, false);

	    if (chart != null) {
		int width = 1600;
		int height = 720;
		chart.setAntiAlias(true);
		chart.setBackgroundPaint(null);

		final ChartRenderingInfo info = new ChartRenderingInfo(
			new StandardEntityCollection());

		response.setContentType("image/png");

		ChartUtilities.writeChartAsPNG(out, chart, width, height, info);
	    }
	} catch (SQLException e) {
	    e.printStackTrace();
	}

    }

}
